﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgileSaude.Entities;
using AgileSaude.Business;
using FBS.Framework;

namespace AgileSaude.Web.Admin
{
    public partial class ServicoEdit : PageBase
    {
        #region Properties
        public int IdServico
        {
            get { return ViewState["IdServico"] == null ? int.MinValue : Convert.ToInt32(ViewState["IdServico"]); }
            set { ViewState["IdServico"] = value; }
        }

        public ServicoEntities Servico
        {
            get { return (ServicoEntities)ViewState["Servico"]; }
            set { ViewState["Servico"] = value; }
        }

        ServicoFacade ServicoFacade = new ServicoFacade();

        public List<ServicoEntities> ListaServico
        {
            get { return ViewState["ListaServico"] == null ? new List<ServicoEntities>() : (List<ServicoEntities>)ViewState["ListaServico"]; }
            set { ViewState["ListaServico"] = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Resultado resultado = new Resultado();

                TiposervicoEntities FiltroTiposervico = new TiposervicoEntities();

                ddlTipoServico.DataTextField = "Descricao";
                ddlTipoServico.DataValueField = "IdTipoServico";

                ddlTipoServico.DataSource = new TipoServicoFacade().Listar(FiltroTiposervico, ref resultado).OrderBy(x => x.Descricao);
                ddlTipoServico.DataBind();

                if (Context.Handler is ListaServicos)
                {
                    int IdServico = ((ListaServicos)Context.Handler).IdServico;

                    if (IdServico > 0)
                    {
                        Servico = ServicoFacade.GetServico(IdServico);

                        if (Servico != null)
                        {
                            hdfIdServico.Value = Servico.idServico.ToString();
                            txtNome.Text = Servico.Nome;
                            txtDescricao.Text = Servico.Descricao;
                            ddlTipoServico.SelectedValue = Servico.idTipoServico.ToString();
                        }
                    }

                    if (((ListaServicos)Context.Handler).ReadOnly)
                        btnSalvar.Visible = false;
                }
            }
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/ListaServicos.aspx", false);
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            Resultado resultado = new Resultado();
            string msgConfirmacao = string.Empty;

            if (Page.IsValid)
            {
                try
                {
                    if (Servico == null || Servico.idServico == 0)
                        Servico = new ServicoEntities();

                    Servico.Nome = txtNome.Text;
                    Servico.Descricao = txtDescricao.Text;
                    Servico.idTipoServico = int.Parse(ddlTipoServico.SelectedValue);

                    if (Servico.idServico > 0)
                    {
                        //Alterar
                        resultado = ServicoFacade.Alterar(Servico);
                    }
                    else
                    {
                        //Incluir
                        resultado = ServicoFacade.Incluir(Servico);
                    }

                }
                catch (Exception ex)
                {
                    new ResultadoFacade().GravaLog(ref resultado, ex.Message, "AgileSaude", "", "btnSalvar_Click", "ServicoEdit.aspx");
                }

                if (resultado.Sucesso)
                {
                    Response.Redirect("~/Admin/ListaServicos.aspx", false);
                }
                else
                {
                    ((Label)Master.FindControl("lblMensagem")).Text = resultado.Mensagens[0].Descricoes[0];
                    ((Label)Master.FindControl("lblMensagem")).CssClass = "failureNotification";
                    ((Label)Master.FindControl("lblMensagem")).Visible = true;
                }
            }

        }
    }
}